/**
 * Created by 冠东 on 2017/10/24.
 */
mainApp.factory("customsFactory",function(){
    var service={};
    //获取全部列表或者据条件查询
    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 3
        }
        var list = "";
        RequestUtils.postJson("../cm/get",obj).done(function(resp){
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint("列表获取","失败");
            }
        });

        //console.log(list);
        return list;
    };

    //新增
    service.addCustoms=function(obj){
        console.log(obj);
        RequestUtils.postJson('../cm/add',obj).done(function (resp) {
            if(resp.resultCode==0){
                CommonUtils.buildFailHint('记录新增', '成功');
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('记录新增', '失败');
            }
        });
    }

    //删除
    service.delete=function(id){

        RequestUtils.postJson('../cm/delete',id).done(function (resp) {
            console.log(id);
            if(resp.resultCode==0){
                alert("成功");
                location.reload(true);
            }else{
                alert("失败")
            }

        });;
    }

    //按id查询费用保单service
    service.selectCustomById=function(id){
        console.log("selectCustomById");
        var list="";
        RequestUtils.getJson("../cm/getById/"+id).done(function(resp){
            if(resp.resultCode==0){
                console.log("list");
                console.log(list);
                list=resp.data;
                CommonUtils.buildFailHint('记录修改', '成功');
            }else{
                CommonUtils.buildFailHint('记录修改', '失败');
            }
        });
        return list;
    }

    //修改报单service
    service.updateCustoms=function(customs){
        console.log(customs);
        RequestUtils.postJson("../cm/update/",customs).done(function(resp){
            if(resp.resultCode==0){
                CommonUtils.buildFailHint('记录修改', '成功');
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('记录修改', '失败');
            }
        });
    };

    return service;


})

mainApp.controller("customsController",function($scope,$http,customsFactory){

    $scope.addData;//添加数据对象
    $scope.updateData;//修改数据对象
    $scope.customData;

    $scope.dateTime=CommonUtils.getDateTime();
    $scope.list=customsFactory.getListData(1);
    $scope.customList=$scope.list.items;

    $scope.openAddCustomsModal=function(){
        $("#addCustomsModal").modal("show");
    }
    $scope.openSelectCustomsModal=function(){
        $("#selectCustomsModal").modal("show");
    }

    //
    //$('#datetimepicker').datetimepicker({
    //    format: 'yyyy-mm-dd'      /*此属性是显示顺序，还有显示顺序是mm-dd-yyyy*/
    //});
    CommonUtils.setDatetimepicker("datetimepicker");

    $scope.add=function(addData){
        console.log(addData);
        customsFactory.addCustoms(addData);

        $("#addCustomsModal").modal("hide");
        $scope.$apply();
    }

    $scope.customs;
    //修改的查询
    $scope.selectCustoms=function(id){
        console.log(id);
        $scope.customs=customsFactory.selectCustomById(id);
        console.log("要修改的customs");
        console.log($scope.customs);

        $("#updateCustomsModal").modal("show");
    }

    //修改更新
    $scope.Update=function(customs){
        customsFactory.updateCustoms(customs);

        $("#updateCustomsModal").modal("hide");
        $scope.$apply();
    };

    $scope.select=function(customData){
        $scope.customList=customsFactory.getListData(customData.payee,customData.type);
    }

    $scope.delete=function(id){

        customsFactory.delete(id);
    }

    //翻页
    $scope.NextListData=function(pageIndex){
        $scope.list=customsFactory.getListData(pageIndex);
        $scope.customList=$scope.list.items;
    }
})

